// pages/home/home-nav/medical-condition/bmi-control/index.ts
import { formatTime } from "../../../../../utils/util"
import { getBMI, getStatus, getRecord } from '../../../../../utils/api/BMIControl'
import userBaseInfoState from '../../../../../stores/userBaseInfo'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    patientUid: <string | number>123,
    nowDate: '',
    // TODO: 需要根据数据库设计指定字段
    bmiDetail: <BmiItem | null>{
      patientUid: 123,
      bmimeasurementDate: '2021-08-09',
      height: 170,
      weight: 57.9,
      BMI: 20,
    },
    bmiDetailList: <BmiItem[] | null>[
      {
        patientUid: 123,
        bmimeasurementDate: '2022-08-09',
        height: 171,
        weight: 57.9,
        BMI: 20,
      },
      {
        patientUid: 123,
        bmimeasurementDate: '2022-08-19',
        height: 172,
        weight: 55.9,
        BMI: 20,
      },
      {
        patientUid: 123,
        bmimeasurementDate: '2023-08-19',
        height: 172,
        weight: 59.9,
        BMI: 20,
      },
    ]
  },

  updateBMI(patientUid: string | number) {
    getBMI(patientUid).then((res: any) => {
      // console.log('BMI请求成功', res);
      this.setData({
        'bmiDetail.height': res.data.height,
        'bmiDetail.weight': res.data.weight,
        'bmiDetail.BMI': res.data.bmi,
        'bmiDetail.bmimeasurementDate': res.data.measurementDate
      })
    })
  },

  updateStatus(patientUid: string | number) {
    getStatus(patientUid).then((res: any) => {
      let bmiStatus = res.data.bmiStatus;
      let chart = this.selectComponent('#bmiChartBar');
      chart.changeOptionShow(bmiStatus);
    }).catch((err: any) => {
      console.log('bmi状态请求失败', err);
    })
  },

  updateRecord(patientUid: string | number) {
    getRecord(patientUid).then((res: any) => {
      const data = res.data;

      // 生成 ChartData 数组
      const ChartData: ChartData = {
        categories: data.map((item: any) => item.measurementDate),
        data: data.map((item: any) => item.bmi),
      };

      // 生成倒序的 bmiDetailList 数组
      const bmiDetailList: BmiItem[] = data.reverse().map((item: any) => ({
        patientUid: patientUid,
        bmimeasurementDate: item.measurementDate,
        height: item.height,
        weight: item.weight,
        BMI: item.bmi,
      }));

      let chart = this.selectComponent('#bmiChart')

      this.setData({
        bmiDetailList: bmiDetailList
      });

      chart.updateChartData(ChartData);
    })
  },

  addBmiRecord() {
    console.log('添加记录');
    wx.navigateTo({
      url: './add-bmi/index'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 获取今日日期
    const date = new Date();
    this.data.nowDate = formatTime(date, 'yyyy-MM-dd');
    this.setData({
      patientUid: userBaseInfoState.data.patientUid,
      nowDate: this.data.nowDate
    })
    this.updateBMI(this.data.patientUid);
    this.updateStatus(this.data.patientUid);
    this.updateRecord(this.data.patientUid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取今日日期
    const date = new Date();
    this.data.nowDate = formatTime(date, 'yyyy-MM-dd');
    this.setData({
      patientUid: userBaseInfoState.data.patientUid,
      nowDate: this.data.nowDate
    })
    this.updateBMI(this.data.patientUid);
    this.updateStatus(this.data.patientUid);
    this.updateRecord(this.data.patientUid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})